#include <vector>
#include <set>
#include <iostream>
#include <unordered_map>
#include <numeric>

using namespace std;

string reverseWords(string s) {
    int pre = 0, i = 0, end = s.size();
    while(i <= end){
        while(s[i] == 32 && i <= end){
            pre++; i++;
        }
        while(s[i] != 32 && i < end){
            i++;
        }
        if(i > pre){s.insert(end, " "+s.substr(pre, i-pre));}
        s.erase(0, i);
        end -= i; i = 0; pre = 0;
    }
    return s;
}

int main(){
    string s = "  a good   example  ";
    string ret = reverseWords(s);
    cout << ret << endl;
    return 0;
}